<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/5/7
 * Time: 15:00
 */

namespace app\admin\model;


use think\Model;

class Menu extends Model
{
    /**
     * 根据条件获取礼物列表信息
     * @param $where
     * @param $offset
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getMenuByWhere($where, $offset, $limit)
    {
        $res = $this->where($where)->limit($offset, $limit)->select();
        //取出我的信息
        array_shift($res);
        return $res;
    }

    /**
     * 根据搜索条件获取所有的礼物数量
     * @param $where
     * @return int|string
     */
    public function getAllMenu($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 添加礼物数据
     * @param $data
     * @return array|bool
     */
    public function addMenu($data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }
        try {
            $data['pid'] = 0;
            $result = $this->save($data);
            if ($result === false) {
                return msg(-1, '', $this->getError());
            } else {
                return msg(1, url('Menu/index'), '添加菜单成功！');
            }
        } catch (\PDOException $e) {
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑菜单数据
     * @param $data
     * @return array|bool
     */
    public function editMenu($data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }
        try {

            $result = $this->save($data, array('id' => $data['id']));
            if ($result === false) {
                return msg(-1, '', $this->getError());
            } else {
                return msg(1, url('Menu/index'), '编辑菜单成功！');
            }
        } catch (\PDOException $e) {
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 获取单个菜单数据
     * @param $id
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOneMenu($id)
    {

        if (empty($id)) {
            return false;
        }

        return $this->find($id);
    }

    /**
     * 删除数据
     * @param $id
     * @return array|bool
     */
    public function saveStatus($id)
    {
        if (empty($id)) {
            return false;
        }
        try {
            $res = $this->save(array('status' => 2), array('id' => $id));
            if ($res === false) {
                return msg(-1, '', $this->getError());
            } else {
                return msg(1, url('Menu/index'), '删除菜单成功！');
            }
        } catch (\PDOException $e) {
            return msg(-2, '', $e->getMessage());
        }
    }
}